package cn.vshare.controller;

import cn.vshare.entity.Barrage;
import cn.vshare.entity.User;
import cn.vshare.service.BarrageService;
import cn.vshare.util.SensitiveWordUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BarrageController {

    @Resource(name = "barrageServiceImpl")
    private BarrageService barrageService;

 /*   //弹幕  增
    @RequestMapping("/addBarrage.do")
    public boolean addBarrage(@RequestBody Barrage barrage) {
        return barrageService.addBarrage(barrage);
    }*/

    //弹幕  删
    @RequestMapping("/delBarrage.do")
    public boolean delBarrage(Barrage barrage) {
        return barrageService.delBarrage(barrage);
    }

   /* //弹幕  筛选
    @RequestMapping("/getBarrageListS.do")
    public Map queryBarrageFilterByUser(String videoId, List<String> list) {
        Map map = new HashMap();
        String msg = null;
        List<Barrage> listBarrage = new ArrayList<Barrage>();
        listBarrage = barrageService.queryBarrageFilterByUser(videoId,list);
        map.put("listBarrage",listBarrage);
        map.put("code",1);

        if (list.size()>0){
            msg = "";
        }else {
            msg = "获取弹幕列表失败";
        }
        map.put("msg",msg);
        return map;
    }*/


    //弹幕  查&新增
    @RequestMapping(value = "/getBarrageList.do")
    public Map queryBarrageByVideo(@RequestParam(required = false) String id, @RequestBody(required = false) Barrage barrage, HttpSession session) {
        Map map = new HashMap();
        User user = (User) session.getAttribute("USER");
        if (id != null && !id.equals("")) {
            //String msg = null;
            List data = new ArrayList<Barrage>();
            data = barrageService.queryBarrageByVideo(id);
            map.put("data", data);
            map.put("code", 0);
            //map.put("msg", "");
        }
        if (barrage != null) {
            //System.out.println(barrage.getText());
            //添加文本过滤
            int count=SensitiveWordUtil.checkBadWord(barrage.getText(),0,2);
            if(count>0){
                map.put("code",1);
            }else{
                barrage.setUser(user);
                barrageService.addBarrage(barrage);
                map.put("code",0);
            }

        }


        /*if (list.size()>0){
            msg = "";
        }else {
            msg = "获取弹幕列表失败";
        }
        map.put("msg",msg);*/

        return map;


    }









}
